package com.wwz.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwz.mapper.DeviceMapper;
import com.wwz.pojo.Charge;
import com.wwz.pojo.Device;
import com.wwz.pojo.Personnel;
import com.wwz.pojo.query.DeviceQuery;
import com.wwz.util.JobUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;


/**
 * 设备接口实现
 * @date 2022/03/26 15:11:31
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements IDeviceService {

    @Resource
    private IPersonnelService personnelService;

    @Override
    public IPage<Device> toPage(DeviceQuery query) {
        LambdaQueryWrapper<Device> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(query.getDeviceName())){
            wrapper.like(Device::getDeviceName,query.getDeviceName());
        }
        wrapper.orderByDesc(Device::getCreateTime);
        IPage<Device> page = this.page(query.toPage(), wrapper);

        page.getRecords().forEach(s->{
            Personnel personnel = personnelService.getById(s.getCreateBy());
            if ( personnel != null){
                s.setCreateByName(personnel.getName());
            }
        });

        return page;
    }

    @Override
    public void addOrUpdate(Device device) {

        Device one = this.getById(device);
        if (one == null){
            device.setId(StrUtil.uuid());
            device.setCreateBy(JobUtils.getLogin());
            device.setCreateTime(new Date());
            save(device);
            return;
        }

        updateById(device);
    }
}
